/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.washburn.cm333.project.gui;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
 *
 * @author Midwest Health
 */
import javax.swing.*;

import edu.washburn.cm333.project.entities.Lease;
import edu.washburn.cm333.project.entities.Main;
import edu.washburn.cm333.project.entities.Property;
import edu.washburn.cm333.project.entities.PropertyManager;

public class PropertyForm extends javax.swing.JFrame {

	/**
	 * Creates new form PropertyForm
	 */
	public PropertyForm() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		lblTitle = new javax.swing.JLabel();
		lblAddress = new javax.swing.JLabel();
		lblCity = new javax.swing.JLabel();
		lblState = new javax.swing.JLabel();
		lblZip = new javax.swing.JLabel();
		lblUnitNumber = new javax.swing.JLabel();
		btnSubmit = new javax.swing.JButton();
		btnExit = new javax.swing.JButton();
		txtAddress = new javax.swing.JTextField();
		txtCity = new javax.swing.JTextField();
		txtState = new javax.swing.JTextField();
		txtZip = new javax.swing.JTextField();
		txtUnitNumber = new javax.swing.JTextField();

		setTitle("New Property Form");

		lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
		lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		lblTitle.setText("Add New Property");

		lblAddress.setText("Address");

		lblCity.setText("City");

		lblState.setText("State");

		lblZip.setText("Zip");

		lblUnitNumber.setText("Unit#");

		btnSubmit.setText("Submit");
		btnSubmit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSubmitActionPerformed(evt);
			}
		});

		btnExit.setText("Exit");
		btnExit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnExitActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(lblTitle, javax.swing.GroupLayout.DEFAULT_SIZE,
						406, Short.MAX_VALUE)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(67, 67, 67)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						lblAddress)
																				.addComponent(
																						lblCity)
																				.addComponent(
																						lblState)
																				.addComponent(
																						lblZip)
																				.addComponent(
																						lblUnitNumber))
																.addGap(63, 63,
																		63)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						txtAddress)
																				.addComponent(
																						txtCity)
																				.addComponent(
																						txtState)
																				.addComponent(
																						txtZip)
																				.addComponent(
																						txtUnitNumber,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						178,
																						Short.MAX_VALUE))
																.addContainerGap(
																		56,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(28, 28,
																		28)
																.addComponent(
																		btnSubmit)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		97,
																		Short.MAX_VALUE)
																.addComponent(
																		btnExit)
																.addGap(92, 92,
																		92)))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(lblTitle,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										31,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblAddress)
												.addComponent(
														txtAddress,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblCity)
												.addComponent(
														txtCity,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblState)
												.addComponent(
														txtState,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblZip)
												.addComponent(
														txtZip,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(lblUnitNumber)
												.addComponent(
														txtUnitNumber,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										24, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(btnSubmit)
												.addComponent(btnExit))
								.addGap(31, 31, 31)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
		this.setVisible(false);
	}//GEN-LAST:event_btnExitActionPerformed

	@SuppressWarnings("empty-statement")
	private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
		if (!this.txtAddress.getText().trim().matches("[[a-zA-Z0-9]|^\\s]+")
				|| !this.txtCity.getText().trim().matches("[a-zA-Z]+")
				|| !this.txtState.getText().trim().matches("[[a-zA-Z]|^\\s]+")
				|| !this.txtZip.getText().trim().matches("[0-9]+")
				|| !this.txtUnitNumber.getText().trim().matches("[0-9]+")) {
			JOptionPane
					.showMessageDialog(
							this,
							"All fields are required and must have the apropriate alpha or numeric characters.",
							"Missing or Incorrect Information",
							JOptionPane.WARNING_MESSAGE);
		} else {

			EntityManager em = PropertyManager.getEntityManager();

			em.getTransaction().begin();
			Property property = new Property();
			property.setStreetAddress(txtAddress.getText());
			property.setUnit(txtUnitNumber.getText());
			property.setCity(txtCity.getText());
			property.setState(txtState.getText());
			property.setZip(txtZip.getText());
			em.persist(property);
			em.getTransaction().commit();

			this.setVisible(false);
		}
	}//GEN-LAST:event_btnSubmitActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(PropertyForm.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(PropertyForm.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(PropertyForm.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(PropertyForm.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new PropertyForm().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnExit;
	private javax.swing.JButton btnSubmit;
	private javax.swing.JLabel lblAddress;
	private javax.swing.JLabel lblCity;
	private javax.swing.JLabel lblState;
	private javax.swing.JLabel lblTitle;
	private javax.swing.JLabel lblUnitNumber;
	private javax.swing.JLabel lblZip;
	private javax.swing.JTextField txtAddress;
	private javax.swing.JTextField txtCity;
	private javax.swing.JTextField txtState;
	private javax.swing.JTextField txtUnitNumber;
	private javax.swing.JTextField txtZip;
	// End of variables declaration//GEN-END:variables
}
